package com.woniuxy.controller;

import com.woniuxy.entity.City;
import com.woniuxy.entity.County;
import com.woniuxy.entity.Province;
import com.woniuxy.service.hospital.HospitalService;
import com.woniuxy.service.location.CityService;
import com.woniuxy.service.location.CountyService;
import com.woniuxy.service.location.ProvinceService;
import com.woniuxy.util.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author 马踏东京赏樱花
 */
@RestController
public class LocationController {
    @Autowired
    private CityService cityService;
    @Autowired
    private CountyService countyService;
    @Autowired
    private ProvinceService provinceService;
    @Autowired
    private HospitalService hospitalService;

    @PostMapping("findTree")
    public ResultVo findTree() {
       /* List<Province> provinceList = provinceService.findAll();
        List<City> cityList = cityService.findAll();
        List<County> countyList = countyService.findAll();
        List<Hospital> hospitalList = hospitalService.findAll();

        HashMap<Integer, Province> provinceMap = new HashMap<>();
        HashMap<Integer, City> cityMap = new HashMap<>();
        HashMap<Integer, County> countyMap = new HashMap<>();

        // Build provinceMap
        for (Province province : provinceList) {
            provinceMap.put(province.getProvinceId(), province);
        }

        // Build cityMap and link cities to provinces
        for (City city : cityList) {
            cityMap.put(city.getCityId(), city);
            Province province = provinceMap.get(city.getProvinceId());
            if (province != null) {
                List<City> cities = province.getCityList();
                if (cities == null) {
                    cities = new ArrayList<>();
                    province.setCityList(cities);
                }
                cities.add(city);
            }
        }

        // Build countyMap and link counties to cities
        for (County county : countyList) {
            countyMap.put(county.getCountyId(), county);
            City city = cityMap.get(county.getCityId());
            if (city != null) {
                List<County> counties = city.getCountyList();
                if (counties == null) {
                    counties = new ArrayList<>();
                    city.setCountyList(counties);
                }
                counties.add(county);
            }
        }

        // Link hospitals to counties
        for (Hospital hospital : hospitalList) {
            County county = countyMap.get(hospital.getCountyId());
            if (county != null) {
                List<Hospital> hospitals = county.getHospitalList();
                if (hospitals == null) {
                    hospitals = new ArrayList<>();
                    county.setHospitalList(hospitals);
                }
                hospitals.add(hospital);
            }
        }

        return provinceList != null ? ResultVo.success("ok", provinceList) : ResultVo.failure("no");*/
        return ResultVo.success(provinceService.findAll());
    }



    @PostMapping("city/findOne")
    public ResultVo findOne0(@RequestBody String str) {
        City city = cityService.findOne(str);
        return city != null ? ResultVo.success("ok", city) : ResultVo.failure("no");
    }

    @PostMapping("city/findAll")
    public ResultVo findAll0() {
        List<City> city = cityService.findAll();
        return city.size() > 0 ? ResultVo.success("ok", city) : ResultVo.failure("no");
    }

    @PostMapping("city/addOne")
    public ResultVo addOne0(@RequestBody String str) {
        int city = cityService.addOne(str);
        return city > 0 ? ResultVo.success("ok", city) : ResultVo.failure("no");
    }

    @PostMapping("city/deleteOne")
    public ResultVo deleteOne0(@RequestBody String str) {
        int city = cityService.deleteOne(str);
        return city > 0 ? ResultVo.success("ok", city) : ResultVo.failure("no");
    }

    @PostMapping("city/updateOne")
    public ResultVo updateOne0(@RequestBody City city) {
        City cit = cityService.updateOne(city);
        return cit != null ? ResultVo.success("ok", cit) : ResultVo.failure("no");
    }

    //*************************************************************************************************************************
    @PostMapping("county/findOne")
    public ResultVo findOne1(@RequestBody String str) {
        County county = countyService.findOne(str);
        return county != null ? ResultVo.success("ok", county) : ResultVo.failure("no");
    }

    @PostMapping("county/findAll")
    public ResultVo findAll1() {
        List<County> county = countyService.findAll();
        return county.size() > 0 ? ResultVo.success("ok", county) : ResultVo.failure("no");
    }

    @PostMapping("county/addOne")
    public ResultVo addOne1(@RequestBody String str) {
        int city = countyService.addOne(str);
        return city > 0 ? ResultVo.success("ok", city) : ResultVo.failure("no");
    }

    @PostMapping("county/deleteOne")
    public ResultVo deleteOne1(@RequestBody String str) {
        int city = countyService.deleteOne(str);
        return city > 0 ? ResultVo.success("ok", city) : ResultVo.failure("no");
    }

    @PostMapping("county/updateOne")
    public ResultVo updateOne1(@RequestBody County county) {
        County cou = countyService.updateOne(county);
        return cou != null ? ResultVo.success("ok", cou) : ResultVo.failure("no");
    }

    //*************************************************************************************************************************
    @PostMapping("province/findOne")
    public ResultVo findOne2(@RequestBody String str) {
        Province province = provinceService.findOne(str);
        return province != null ? ResultVo.success("ok", province) : ResultVo.failure("no");
    }

    @PostMapping("province/findAll")
    public ResultVo findAll2() {
        List<Province> province = provinceService.findAll();
        return province.size() > 0 ? ResultVo.success("ok", province) : ResultVo.failure("no");
    }

    @PostMapping("province/addOne")
    public ResultVo addOne2(@RequestBody String str) {
        int province = provinceService.addOne(str);
        return province > 0 ? ResultVo.success("ok", province) : ResultVo.failure("no");
    }

    @PostMapping("province/deleteOne")
    public ResultVo deleteOne2(@RequestBody String str) {
        int province = provinceService.deleteOne(str);
        return province > 0 ? ResultVo.success("ok", province) : ResultVo.failure("no");
    }

    @PostMapping("province/updateOne")
    public ResultVo updateOne2(@RequestBody Province province) {
        Province pro = provinceService.updateOne(province);
        return pro != null ? ResultVo.success("ok", pro) : ResultVo.failure("no");
    }
}
